package com.ruoyi.form.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.form.mapper.FormOrderMapper;
import com.ruoyi.form.domain.FormOrder;
import com.ruoyi.form.service.IFormOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-21
 */
@Service
public class FormOrderServiceImpl implements IFormOrderService 
{
    @Autowired
    private FormOrderMapper formOrderMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public FormOrder selectFormOrderById(Long id)
    {
        return formOrderMapper.selectFormOrderById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param formOrder 订单
     * @return 订单
     */
    @Override
    public List<FormOrder> selectFormOrderList(FormOrder formOrder)
    {
        return formOrderMapper.selectFormOrderList(formOrder);
    }

    /**
     * 新增订单
     * 
     * @param formOrder 订单
     * @return 结果
     */
    @Override
    public int insertFormOrder(FormOrder formOrder)
    {
        formOrder.setCreateTime(DateUtils.getNowDate());
        formOrder.setUpdateTime(new Date());
        formOrder.setOrderNumber(System.currentTimeMillis()+"");
        int i = formOrderMapper.insertFormOrder(formOrder);

        SysUser sysUser = userMapper.selectUserById(formOrder.getUserId());
        if(sysUser != null){
            //更新数量
            Integer sendCount = sysUser.getSendCount();
            if(sendCount != null && sendCount >=0){
                sysUser.setSendCount(formOrder.getQuantity() + sendCount);
                userMapper.updateUser(sysUser);
            }
        }

        return i;
    }

    /**
     * 修改订单
     * 
     * @param formOrder 订单
     * @return 结果
     */
    @Override
    public int updateFormOrder(FormOrder formOrder)
    {
        formOrder.setUpdateTime(DateUtils.getNowDate());
        return formOrderMapper.updateFormOrder(formOrder);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteFormOrderByIds(String ids)
    {
        List<String> list = Arrays.asList(ids.split(","));
        for (String s : list) {
            FormOrder formOrder = formOrderMapper.selectFormOrderById(Long.valueOf(s));
            SysUser sysUser = userMapper.selectUserById(formOrder.getUserId());
            if(sysUser != null){
                Integer sendCount = sysUser.getSendCount();
                if(sendCount != null && sendCount >=0){
                    int count = sendCount - formOrder.getQuantity();
                    if(count <= 0){
                        count = 0;
                    }
                    sysUser.setSendCount(count);
                    userMapper.updateUser(sysUser);
                }
            }
        }
        int i = formOrderMapper.deleteFormOrderByIds(Convert.toStrArray(ids));
        return i;
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteFormOrderById(Long id)
    {
        return formOrderMapper.deleteFormOrderById(id);
    }
}
